package com.home.homepage.service.banner.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eig.common.entity.homepageentity.banner.CrmBanner;
import com.eig.common.vo.Result;
import com.home.homepage.dao.banner.BannerDao;
import com.home.homepage.service.banner.BannerService;
import com.home.homepage.vo.ConstantPropertiesUtils;
import org.apache.ibatis.jdbc.Null;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import org.joda.time.DateTime;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.Date;
import java.util.UUID;

/**
 * @author ：YQ袁（2446806940@qq.com）
 * @date ：Created in 2022/12/16 17:38
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class BannerServiceImpl implements BannerService {
    @Autowired
    private BannerDao bannerDao;
    @Override
    public Result getAllBanner(Integer current, Integer pageSize, CrmBanner crmBanner) {
        IPage<CrmBanner>page = new Page<>(current, pageSize);
        QueryWrapper<CrmBanner> wrapper = new QueryWrapper<>();
        wrapper.eq("is_deleted",0);
        if (StringUtils.hasText(crmBanner.getTitle())) {
            wrapper.like("title", crmBanner.getTitle());
        }
        if (StringUtils.hasText(crmBanner.getIsDeleted())){
            wrapper.eq("is_deleted",crmBanner.getIsDeleted());
        }
        IPage<CrmBanner> page1 = bannerDao.selectPage(page, wrapper);
        if (page1 != null){
            return  new Result(2000,"查询成功" ,page1);
        }
        return new Result<>(5000,"查询失败");
    }

    @Override
    public String uploadFileAvatar( MultipartFile file) {
        //通过工具类获取配置信息
        // Endpoint以上海为例，其它Region请按实际情况填写。
        String endpoint = ConstantPropertiesUtils.END_POIND;
        // 云账号AccessKey有所有API访问权限，建议遵循阿里云安全最佳实践，创建并使用RAM子账号进行API访问或日常运维，请登录 https://ram.console.aliyun.com 创建。
        String accessKeyId = ConstantPropertiesUtils.ACCESS_KEY_ID;
        String accessKeySecret = ConstantPropertiesUtils.ACCESS_KEY_SECRET;
        String bucketname = ConstantPropertiesUtils.BUCKET_NAME;

        try {
            // 创建OSSClient实例。
            OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
            // 获取上传文件流
            InputStream inputStream = file.getInputStream();
           // 文件名：uuid.扩展名
            String original = file.getOriginalFilename();
            //在文件名称里面添加随机唯一的值
            String fileName = UUID.randomUUID().toString();
            String fileType = original.substring(original.lastIndexOf("."));
            String newName = fileName + fileType;
            //把文件按照日期进行分类
            //获取当前日期
            String datePath = new DateTime().toString("yyyy/MM/dd");
            //拼接路径
            fileName = datePath +"/"+ newName;
            //调用oss方法实现上传
            //第一个参数 Bucket 名称, 第二个参数 上传到oss文件路径和文件名称
            ossClient.putObject(bucketname, fileName, inputStream);
            // 关闭OSSClient。
            ossClient.shutdown();
            //把上传之后文件路径返回
            //需要把上传到阿里云oss路径手动拼接出来
            String url = "https://"+ bucketname +"."+ endpoint +"/"+ fileName;
            return url;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    @Override
    public Result addBanner(CrmBanner crmBanner) {

        CrmBanner crm = new CrmBanner();
        crm.setTitle(crmBanner.getTitle());
        crm.setImageUrl("1");
        crm.setIsDeleted("0");
        crm.setIsDisable("0");
        crm.setGmtCreate(crmBanner.getGmtCreate());
        crm.setGmtModified(crmBanner.getGmtModified());
        int insert = bannerDao.insert(crm);
        if (insert>0){
            return new Result(2000,"添加成功");
        }
        return new Result<>(5000,"添加失败");
    }

    @Override
    public Result updateBanner(CrmBanner crmBanner) {

        UpdateWrapper<CrmBanner> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",crmBanner.getId());
        wrapper.set("title",crmBanner.getTitle());
        wrapper.set("image_url",crmBanner.getImageUrl());
        wrapper.set("link_url",crmBanner.getLinkUrl());
        wrapper.set("gmt_create",crmBanner.getGmtCreate());
        wrapper.set("gmt_modified",crmBanner.getGmtModified());
        wrapper.set("sort",crmBanner.getSort());
        int update = bannerDao.update(null, wrapper);
        if (update>0){
            return new Result(2000,"修改成功");
        }
        return new Result(2000,"修改失败");
    }

    @Override
    public Result changeStatus(String id, String isDisable) {
        UpdateWrapper<CrmBanner> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",id);
        wrapper.set("is_disable",isDisable);
        int update = bannerDao.update(null, wrapper);
        if (update>0){
            return new Result(2000,"修改状态成功");
        }
        return new Result(5000,"修改状态失败");
    }

    @Override
    public Result deleteBanner(CrmBanner crmBanner) {
        UpdateWrapper<CrmBanner>wrapper= new UpdateWrapper<>();
        wrapper.eq("id",crmBanner.getId());
        int delete = bannerDao.delete(wrapper);
        if (delete>0){
            return new Result(2000,"删除成功");
        }
        return new Result(5000,"删除失败");
    }
}
